#!/usr/bin/env python
# coding: utf-8

# ### 准备工作

# In[ ]:


import requests,json
import pandas as pd
from PIL import Image
from io import BytesIO
vicky_key ="da5ecbc743c44c3438e42f470754d04c"


# ### 地理编码

# In[2]:


def geocoding(address,city=None,batch=None,sig=None):
    """获取地理编码"""
    url = 'https://restapi.amap.com/v3/geocode/geo?parameters'
    params={
        'key': vicky_key,
        'address':address,
        'city':city,
        'batch':batch,
        'sig':sig
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data


# ### 逆地理编码

# In[20]:


# 1-1 地理编码
def inverse_geocoding(location,radius,roadlevel,extensions):
    """获取地理编码"""
    url = 'https://restapi.amap.com/v3/geocode/regeo?parameters'
    params={
    "key":vicky_key,
    "location":location,
    "output":"json",
    "radius":radius,
    "roadlevel":roadlevel,
    "extensions":extensions    
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data


# ### 路径规划

# #### 步行路径规划

# In[21]:


def walking(origin,destination,sig=None,output="json",callback=None):
    """步行路径规划"""
    walking_url = "https://restapi.amap.com/v3/direction/walking?parameters"
    params = {
        "key": vicky_key,
        "origin": origin,
        "destination": destination,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(walking_url, params=params)
    return r.json()


# #### 公交路线规划

# In[6]:


def traffic_route(origin,destination,city):
    """公交路线规划"""
    geocode_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
        "key":vicky_key,
        "origin":origin,
        "destination":destination,
        "output":"json",
        "city":city
    }
    r = requests.get(geocode_url,params=params)
    result = r.json()
    return result


# ### 行政区域查询

# In[7]:


def admin_area(keywords,extensions):
    """公交路线规划"""
    geocode_url = "https://restapi.amap.com/v3/config/district?parameters"
    params = {
        "key":vicky_key,
        "output":"json",
        "extensions":extensions,
        "keywords":keywords
    }
    r = requests.get(geocode_url,params=params)
    result = r.json()
    return result


# ### 搜索POI

# #### 关键词搜索

# In[9]:


def keyword_search(keywords,city):
    """关键词搜索"""
    geocode_url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
        "key":vicky_key,
        "output":"json",
        "city":city,
        "keywords":keywords
    }
    r = requests.get(geocode_url,params=params)
    result = r.json()
    return result


# #### 周边搜索

# In[10]:


def search_around(location,keywords=None,types="050000|070000|120000",city=None,radius=3000,sortrule="distance",offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    """周边搜索"""
    geocode_url = "https://restapi.amap.com/v3/place/around?parameters"
    params = {
        "key": vicky_key,
        "location": location,
        "keywords": keywords,
        "types": types,
        "city": city,
        "radius": radius,
        "sortrule": sortrule,
        "offset": offset,
        "page": page,
        "extensions": extensions,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(geocode_url, params)
    return r.json()


# #### 多边形搜索

# In[11]:


def polygon_search(keywords,location,city,radius=None):
    """多边形搜索"""
    geocode_url = "https://restapi.amap.com/v3/place/polygon?parameters"
    params = {
        "key":vicky_key,
        "location":location,
        "radius":radius,
        "keywords":keywords,
        "city":city,
        "output":"json"
    }
    r = requests.get(geocode_url,params=params)
    result = r.json()
    return result


# #### ID查询

# In[12]:


def id_search(id):
    """多边形搜索"""
    geocode_url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
        "key":vicky_key,
        "id":id,
        "output":"json"
    }
    r = requests.get(geocode_url,params=params)
    result = r.json()
    return result


# ### IP定位

# In[13]:


def id_location():
    """获取IP定位"""
    url = "https://restapi.amap.com/v3/ip?parameters"
    params = {
        "key":vicky_key,
        "output":"json"
    }
    respond = requests.get(url,params=params)
    data = respond.json()
    return data


# ### 静态地图

# In[14]:


def static_map(location,zoom=15,size="400*400",scale=1,markers=None,labels=None,paths=None,traffic=0,sig=None):
    """获取静态地图"""
    from PIL import Image
    from io import BytesIO
    url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
        "key":vicky_key,
        "location":location,
        "zoom":zoom,#地图缩放值[1,17]
        "size":size,#最大值为1024*1024
        "scale":scale,
        "markers":markers,# 标注数最大为10个
        "labels":labels,#标签数最大为10个
        "paths":paths,# 折线最大为4个
        "traffic":traffic,#是否展现实时路况:0，不展现；1，展现
        "output":"json"
    }
    respond = requests.get(url,params=params)
    data = Image.open(BytesIO(respond.content))
    return data


# ### 坐标转换

# In[15]:


def transformation(locations,coordsys="",sig=""):
    """坐标转换"""
    url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
        "key":vicky_key,
        "locations":locations,
        "coordsys":coordsys,
        "sig":sig,
        "output":"json"
    }
    respond = requests.get(url,params=params)
    data = respond.json()
    return data


# ### 天气查询

# In[16]:


def weather_forecast(city,extensions=""):
    """获取天气查询"""
    url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key":vicky_key,
        "city":city,#填写城市编码
        "extensions":extensions,#base:返回实况天气，all:返回预报天气（可选参数）
        "output":"json"
    }
    respond = requests.get(url,params=params)
    data = respond.json()
    return data


# ### 输入提示

# In[17]:


def input_prompt(keywords,type = "",location="",city="",citylimit="",datatype="",sig="",callback=""):
    """获取输入提示"""
    url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key":vicky_key,
        "keywords":keywords,
        "type":type,
        "location":location,
        "city":city,
        "citylimit":citylimit,
        "datatype":datatype,
        "sig":sig,
        "callback":callback,
        "output":"json"
    }
    respond = requests.get(url,params=params)
    data = respond.json()
    return data


# ### 交通态势

# #### 矩形交通态势

# In[18]:


def traffic_situation(rectangle,extensions = "",sig="",level="",callback=""):
    """获取矩形交通态势"""
    url = "https://restapi.amap.com/v3/traffic/status/rectangle?parameters"
    params = {
        "key":vicky_key,
        "level":level,
        "extensions":extensions,
        "sig":sig,
        "rectangle":rectangle,
        "callback":callback,
        "output":"json"
    }
    respond = requests.get(url,params=params)
    data = respond.json()
    return data


# ### 地理围栏

# #### 创建地理围栏

# In[19]:


def creat_rail(name,repeat="",radius = "",points ="",center="",valid_time ="",enable="",fixed_date ="",time="",desc="",alert_condition=""):
    """获取地理围栏"""
    url = "https://restapi.amap.com/v4/geofence/meta?key="+vicky_key
    params = {
        "key":vicky_key,
        "center":center,
        "radius":radius,
        "points":points,
        "name":name,
        "enable":enable,
        "valid_time ":valid_time,
        "repeat":repeat,
        "fixed_date":fixed_date,
        "time":time,
        "desc":desc,
        "alert_condition":alert_condition,
        "output":"json"
    }
    respond = requests.get(url,params=params)
    data = respond.json()
    return data


# #### 查询围栏

# In[ ]:


def inquire_rail(id="",gid="",name="",page_no="",page_size="",enable="",start_time="",end_time=""):
    """获取地理围栏"""
    url = "https://restapi.amap.com/v4/geofence/meta?key="+vicky_key
    params = {
        "key":vicky_key,
        "id":id,
        "gid":gid,
        "name":name,
        "page_no":page_no,
        "page_size":page_size,
        "enable":enable,
        "start_time":start_time,
        "end_time":end_time,
    }
    respond = requests.get(url,params=params)
    data = respond.json()
    return data

